package com.atls.eduservice.controller.front;

import com.atls.commonutils.JwtUtils;
import com.atls.commonutils.R;
import com.atls.commonutils.ordervo.CourseWebVoOrder;
import com.atls.eduservice.client.OrdersClient;
import com.atls.eduservice.entity.EduCourse;
import com.atls.eduservice.entity.chapter.ChapterVo;
import com.atls.eduservice.entity.vo.frontvo.CourseFrontVo;
import com.atls.eduservice.entity.vo.frontvo.CourseWebVo;
import com.atls.eduservice.service.EduChapterService;
import com.atls.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/eduservice/coursefront")
@CrossOrigin
public class CourseFrontController {

    @Autowired
    private EduCourseService courseService;

    @Autowired
    private EduChapterService chapterService;

    @Autowired
    private OrdersClient ordersClient;


    //1.条件分页查询课程
    @PostMapping("getCourseFrontList/{page}/{limit}")
    public R getCourseFrontList(@PathVariable Long page,
                                @PathVariable Long limit,
                                @RequestBody(required = false) CourseFrontVo courseFrontVo){

        Page<EduCourse> pageCourse=new Page<>(page,limit);
        Map<String,Object> map=courseService.getFrontCourseList(pageCourse,courseFrontVo);
        //返回分页所有数据
        return R.ok().data(map);
    }

    //2 课程详情的方法
    @GetMapping("getFrontCourseInfo/{courseId}")
    public R getFrontCourseInfo(@PathVariable String courseId, HttpServletRequest request){
        //根据课程id，编写sql语句查询课程信息
        CourseWebVo courseWebVo=courseService.getBaseCourseInfo(courseId);
        //根据课程id查询章节和小节
        List<ChapterVo> chapterVoList = chapterService.getChapterVideoByCourseId(courseId);

        //JwtUtils.getMemberIdByJwtToken(request);
        //判断用户是否已经登录
        String token = request.getHeader("token");
        if(!StringUtils.isEmpty(token)){
            //根据课程id和用户id查询课程是否被支付
            boolean buyCourse = ordersClient.isBuyCourse(courseId, JwtUtils.getMemberIdByJwtToken(request));
            return R.ok().data("courseWebVo",courseWebVo).data("chapterVoList",chapterVoList).data("isBuy",buyCourse);
        }

        return R.ok().data("courseWebVo",courseWebVo).data("chapterVoList",chapterVoList);
    }

    /**
     * 2021/04/06
     * @param id
     * @return
     */
    //根据课程id查询课程信息
    //用于生成订单
    @PostMapping("getCourseInfoOrder/{id}")
    public CourseWebVoOrder getCourseInfoOrder(@PathVariable String id){
        CourseWebVo courseInfo = courseService.getBaseCourseInfo(id);
        CourseWebVoOrder courseWebVoOrder = new CourseWebVoOrder();
        BeanUtils.copyProperties(courseInfo,courseWebVoOrder);
        return courseWebVoOrder;
    }








}
